package com.mao.dao.product;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.dao.BaseDAOHibernate;
import com.mao.model.product.Product;
import com.mao.model.voucher.ProductDeliveryVoucher;
import com.mao.model.warehouse.WareHouse;
import com.mao.service.impl.product.ProductCondition;
import com.mao.util.Validator;

@Component("productDao")
public class ProductDao extends BaseDAOHibernate {

	public Product saveProduct(Product pro) {
		getHibernateTemplate().save(pro);
		return pro;
	}

	public Product updateProduct(Product pro) {
		getHibernateTemplate().update(pro);
		return pro;
	}

	public List<Product> getAllProducts(ProductCondition productCondition, Integer page, Integer rows) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("from Product p where 1 = 1 and p.status = " + Product.STATUS_NORMAL);
		if (!Validator.isEmpty(productCondition.getStoreid())) {
			hql.append(" and p.store.id = " + productCondition.getStoreid());
		}
//		if (productCondition.getPcid() != null) {
//			hql.append(" and p.proClass.id = " + productCondition.getPcid());
//		}
		if(!Validator.isEmpty(productCondition.getName()))
			hql.append(" and p.name like '%"+productCondition.getName().trim()+"%'");
		if(!Validator.isEmpty(productCondition.getTreePath())){
			hql.append(" and p.proClass.treePath like '%"+productCondition.getTreePath()+"%'");
		}
		hql.append(" order by p.createTime desc");
		Query query = session.createQuery(hql.toString());
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		List<Product> result = query.list();
		for (int i = 0; i < result.size(); i++) {
			if (!Hibernate.isInitialized(result.get(i).getProClass())) {
				Hibernate.initialize(result.get(i).getProClass());
			}
		}
		for (int i = 0; i < result.size(); i++) {
			if (!Hibernate.isInitialized(result.get(i).getInitWareHouse())) {
				Hibernate.initialize(result.get(i).getInitWareHouse());
			}
		}
		return result;
	}
	
	// 获取该商家下的所有商品数量
	public int getAllProductCount(ProductCondition productCondition) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("select count(p.id) from Product p where 1 = 1 and p.status = " + Product.STATUS_NORMAL);
		if (!Validator.isEmpty(productCondition.getStoreid())) {
			hql.append(" and p.store.id = " + productCondition.getStoreid());
		}
//		if (productCondition.getPcid() != null) {
//			hql.append(" and p.proClass.id = " + productCondition.getPcid());
//		}
		if(!Validator.isEmpty(productCondition.getName()))
			hql.append(" and p.name like '%"+productCondition.getName()+"%'");
		if(!Validator.isEmpty(productCondition.getTreePath())){
			hql.append(" and p.proClass.treePath like '%"+productCondition.getTreePath()+"%'");
		}
	
		Query query = session.createQuery(hql.toString());
		return ((Long)query.uniqueResult()).intValue(); 
	}

	
	public List<Product> getAllProducts(ProductCondition productCondition) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("from Product p where 1 = 1 and p.status = " + Product.STATUS_NORMAL);
		if (!Validator.isEmpty(productCondition.getStoreid())) {
			hql.append(" and p.store.id = " + productCondition.getStoreid());
		}
//		if (!Validator.isEmpty(productCondition.getPcid())) {
//			hql.append(" and p.proClass.id = " + productCondition.getPcid());
//		}
		if(!Validator.isEmpty(productCondition.getName()))
			hql.append(" and p.name like '%"+productCondition.getName()+"%'");
		if(!Validator.isEmpty(productCondition.getTreePath())){
			hql.append(" and p.proClass.treePath like '%"+productCondition.getTreePath()+"%'");
		}
		Query query = session.createQuery(hql.toString());
		List<Product> result = query.list();
		for (int i = 0; i < result.size(); i++) {
			if (!Hibernate.isInitialized(result.get(i).getProClass())) {
				Hibernate.initialize(result.get(i).getProClass());
			}
		}
		return result;
	}
	
	//获取商品列表，用于导出。注：pcid的条件是要带上的
	public List<Product> getProductsForExport(ProductCondition productCondition) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("from Product p where 1 = 1 and p.status = " + Product.STATUS_NORMAL);
		if (!Validator.isEmpty(productCondition.getStoreid())) {
			hql.append(" and p.store.id = " + productCondition.getStoreid());
		}
		if (!Validator.isEmpty(productCondition.getPcid())) {
			hql.append(" and p.proClass.treePath like '%"+productCondition.getTreePath()+"%'");
		}
		if(!Validator.isEmpty(productCondition.getName()))
			hql.append(" and p.name like '%"+productCondition.getName()+"%'");
		
		Query query = session.createQuery(hql.toString());
		List<Product> result = query.list();
		for (int i = 0; i < result.size(); i++) {
			if (!Hibernate.isInitialized(result.get(i).getProClass())) {
				Hibernate.initialize(result.get(i).getProClass());
			}
		}
		return result;
	}
	

	public Product getProductById(long id) {
		Session session = this.getSession();
		String hql = "from Product p where p.id=:id";
		Query query = session.createQuery(hql);
		query.setParameter("id", id);
		List<Product> result = query.list();
		if (result != null) {
			Product product = result.get(0);
			if(!Hibernate.isInitialized(product.getInitWareHouse())){
				Hibernate.initialize(product.getInitWareHouse());
			}
			return product;
		}
		
		return null;
	}

	public List<Product> getProductsByProClass(ProductCondition productCondition, Integer page,
			Integer rows) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("from Product p where 1=1 and p.status = :status");
		hql.append(" and p.store.id=" + productCondition.getStoreid());
		if(!Validator.isEmpty(productCondition.getName())){
			hql.append(" and p.name like '%" + productCondition.getName() + "%'");
		}
		if(!Validator.isEmpty(productCondition.getTreePath())){
			hql.append(" and p.proClass.treePath like '%" + productCondition.getTreePath() + "%'");
		}
		Query query = session.createQuery(hql.toString());
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		query.setParameter("status", Product.STATUS_NORMAL);
		List<Product> result = query.list();
		for (int i = 0; i < result.size(); i++) {
			if (!Hibernate.isInitialized(result.get(i).getProClass())) {
				Hibernate.initialize(result.get(i).getProClass());
			}
		}
		return result;
	}
	
	public Integer getProductsByProClassCount(ProductCondition productCondition) {
		Session session = this.getSession();
		StringBuilder hql = new StringBuilder();
		hql.append("from Product p where 1=1 and p.status = :status");
		hql.append(" and p.store.id=" + productCondition.getStoreid());
		if(!Validator.isEmpty(productCondition.getName())){
			hql.append(" and p.name like '%" + productCondition.getName() + "%'");
		}
		if(!Validator.isEmpty(productCondition.getTreePath())){
			hql.append(" and p.proClass.treePath like '%" + productCondition.getTreePath() + "%'");
		}
		Query query = session.createQuery(hql.toString());
		query.setParameter("status", Product.STATUS_NORMAL);
		return ((Long)query.uniqueResult()).intValue();
	}

	public String valDelPro(long id) {
		Session session = this.getSession();
		String hql = "from ProductDeliveryVoucher pdv,ProductWareHouseVoucher whv where (pdv.product.id = :id or whv.product.id = :id) and pdv.status=:status and whv.status=:status";
		Query query = session.createQuery(hql);
		query.setParameter("id", id);
		query.setParameter("status", ProductDeliveryVoucher.STATUS_NORMAL);
		List list = query.list();
		if (list.size() > 0) {
			return "1";
		}
		return "0";
	}
	
	public List<Product> getAllProducts() {
		Session session = this.getSession();
		String hql = "from Product";
		Query query = session.createQuery(hql);
		return query.list();
	}
	
	public Product getProductByInitAndProId(Long proId,WareHouse warehouse) {
		Session session = this.getSession();
		String hql = "from Product p where p.id=:id and p.initWareHouse.id=:whid";
		Query query = session.createQuery(hql);
		query.setParameter("id", proId);
		query.setParameter("whid", warehouse.getId());
		List<Product> list = query.list();
		if(list.size() > 0){
			return list.get(0);
		}
		return null;
	}

}
